package com.linuxone.iching.server;

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.Random;

import org.apache.commons.digester.Digester;
import org.xml.sax.SAXException;

import com.linuxone.iching.shared.AOU;
import com.linuxone.iching.shared.AOUs;
import com.linuxone.iching.shared.Explain;
import com.linuxone.iching.shared.Gua;
import com.linuxone.iching.shared.IchingSet;
import com.linuxone.iching.shared.Statement;



public class GUATools {
	
	static public IchingSet getIchingSet(){
		Digester digester = new Digester();
		digester.addObjectCreate("iching", IchingSet.class);
		
		digester.addObjectCreate("iching/gua", Gua.class);
		digester.addBeanPropertySetter("iching/gua/id", "id");
		digester.addBeanPropertySetter("iching/gua/name/shortname", "shortName");
		digester.addBeanPropertySetter("iching/gua/name/fullname", "fullName");

		digester.addObjectCreate("iching/gua/statement", Statement.class);
		digester.addBeanPropertySetter("iching/gua/statement/original", "original");
		digester.addBeanPropertySetter("iching/gua/statement/translate", "translate");
		digester.addBeanPropertySetter("iching/gua/statement/remark", "remark");
		digester.addObjectCreate("iching/gua/statement/explain", Explain.class);
		digester.addBeanPropertySetter("iching/gua/statement/explain", "value");
		digester.addSetNext("iching/gua/statement/explain", "addExplain");
		digester.addSetNext("iching/gua/statement", "setStatement");
		
		digester.addBeanPropertySetter("iching/gua/img/front", "front");
		digester.addBeanPropertySetter("iching/gua/img/back", "back");
		digester.addBeanPropertySetter("iching/gua/img/aou", "aou");
		digester.addBeanPropertySetter("iching/gua/symbol", "symbol");
		
		digester.addObjectCreate("iching/gua/aous",AOUs.class);
		digester.addObjectCreate("iching/gua/aous/aou", AOU.class);
		digester.addBeanPropertySetter("iching/gua/aous/aou/name", "name");
		digester.addObjectCreate("iching/gua/aous/aou/statement", Statement.class);
		digester.addBeanPropertySetter("iching/gua/aous/aou/statement/original", "original");
		digester.addBeanPropertySetter("iching/gua/aous/aou/statement/translate", "translate");		
		digester.addObjectCreate("iching/gua/aous/aou/statement/explain", Explain.class);
		digester.addBeanPropertySetter("iching/gua/aous/aou/statement/explain", "value");
		digester.addSetNext("iching/gua/aous/aou/statement/explain", "addExplain");
		digester.addSetNext("iching/gua/aous/aou/statement", "setStatement");
		digester.addSetNext("iching/gua/aous/aou", "addAOU");
		digester.addSetNext("iching/gua/aous", "setAOUs");
		
		digester.addSetNext("iching/gua", "addGua");

		IchingSet iching = null;
		try {
			iching = (IchingSet) digester
					.parse(new InputStreamReader(GUATools.class.getClassLoader()
							.getResourceAsStream("iching.xml"), "utf-8"));
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return iching;
	}
	
	static public int genGUA() {
		Random randomGenerator = new Random();
		return randomGenerator.nextInt(64)+1;
	}
	
	static public int genTouchAOU(){
		Random randomGenerator = new Random();
		return randomGenerator.nextInt(6);
	}
}
